package com.zero.guava.samples.ListenableFuture;

import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import org.junit.Test;

import java.util.concurrent.Callable;
import java.util.concurrent.Executors;

public class ListenableFutureDemo {

    @Test
    public void testListenableFuture() {

        int NUM_THREADS = 10;//10个线程
        ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUM_THREADS));
        final ListenableFuture<String> listenableFuture = executorService.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println("listenableFuture");
                return null;
            }
        });
        listenableFuture.addListener(new Runnable() {
            @Override
            public void run() {
                //在Future任务完成之后运行的一些方法
                System.out.println("methodToRunOnFutureTaskCompletion");
            }
        }, executorService);
    }
}
